<%@page import="java.util.HashMap"%>
<%@page import="cn.sendto.service.UserInfoService"%>
<%@page import="cn.sendto.model.UserInfoModel"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="../err.jsp"%>
    <jsp:include page="../lock.jsp"></jsp:include>
<%
	request.setCharacterEncoding("UTF-8");

	String username = request.getParameter("username");
	String userpass = request.getParameter("userpass");
	String comuserpass = request.getParameter("comuserpass");
	String name = request.getParameter("name");
	String usertel = request.getParameter("usertel");
	
	 //验证数据的有效性
    ArrayList<Integer> errlist = new ArrayList<Integer>();
    HashMap<String,String> formlist = new HashMap<String,String>();
    if(username == null || username.equals("")){
   	 errlist.add(1);
    }else{
    	formlist.put("username",username);
    }
    if(userpass == null || userpass.equals("")){
   	 errlist.add(2);
    }
    if(comuserpass == null || comuserpass.equals("")){
   	 errlist.add(3);
    }
    if(userpass != null && comuserpass != null && !userpass.equals("") && !comuserpass.equals("") && !userpass.equals(comuserpass)){
   	 errlist.add(4);
    }
    if(name == null || name.equals("")){
   	 errlist.add(5);
    }else{
    	formlist.put("name", name);
    }
    if(usertel == null || usertel.equals("")){
   	 errlist.add(6);
    }else{
    	formlist.put("usertel", usertel);
    }
     if(errlist.size()>0){
   	 request.setAttribute("info", errlist);
   	 request.setAttribute("form", formlist);
   	 request.getRequestDispatcher("adduser.jsp").forward(request, response);
   	 return;
    } 
    
     
    //存储数据
    UserInfoModel uim = new UserInfoModel();
    uim.setUserName(username);
    uim.setUserPass(userpass);
    uim.setName(name);
	uim.setUsertel(usertel);
	
	UserInfoService uis =new UserInfoService();
    int rows = uis.add(uim);
    if(rows > 0){
    //返回注册页面
   	 request.setAttribute("adduserinfo", "添加成功，可以继续添加用户或者"+"<a href='userlist.jsp'>点击此处返回业主列表</a>");
   	 request.getRequestDispatcher("adduser.jsp").forward(request, response);
   	 return;
    }else if(rows == -1){
    	 request.setAttribute("adduserinfo", "添加失败,用户名已存在");
    	 request.setAttribute("form", formlist);
       	 request.getRequestDispatcher("adduser.jsp").forward(request, response);
       	 return;
    }
    else{
   	 request.setAttribute("adduserinfo", "添加失败。");
   	 request.getRequestDispatcher("adduser.jsp").forward(request, response);
   	 return;
    }
%>